package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.mapper.DeviceMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @ Description: 设备数据实现层
 * @ BelongsProject: zzyl-cp
 * @ Author: G攀
 * @ CreateTime: 2024-09-12  12:01
 * @ Version: 1.0
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 分页获取设备数据
     */
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto pageReqDto) {
        // 开启分页
        PageHelper.startPage(pageReqDto.getPageNum(), pageReqDto.getPageSize());

        // 前端传来的是时间戳，注意需要转换成LocalDateTime再去数据库中进行查询
        Long startTime = pageReqDto.getStartTime();
        Long endTime = pageReqDto.getEndTime();

        // 时间戳不为空，转换成LocalDateTime再进行赋值
        pageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        pageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));

        // 查询
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(pageReqDto);

        // 封装返回值
        return PageResponse.of(page, DeviceDataVo.class);
    }
}
